package com.example.demo.controller;

import com.example.demo.Util.BaseUtil;
import com.example.demo.entity.MonitorobjInfo;
import com.example.demo.entity.MonitorobjPageInfo;
import com.example.demo.entity.ResultVo;
import com.example.demo.mapper.MonitorobjInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by tanjun on 2021-12-05.
 */
@Controller
public class MonitorController {

    @Autowired
    MonitorobjInfoMapper monitorobjInfoMapper;

    @RequestMapping("/index")
    public String index(Model model,HttpServletRequest request){
        String userName= BaseUtil.getCurrentLoginUserName();
        model.addAttribute("userName", userName);
        return "index";
    }

    @RequestMapping("/getMonitorObjList")
    @ResponseBody
    public Map getMonitorObjList(HttpServletRequest request){
        MonitorobjPageInfo monitorobjPageInfo=new MonitorobjPageInfo();
        monitorobjPageInfo.setPageNumber(Integer.valueOf(request.getParameter("page")));
        monitorobjPageInfo.setPageSize(Integer.valueOf(request.getParameter("rows")));
        List<MonitorobjInfo> lists=monitorobjInfoMapper.queryMonitorobjInfos(monitorobjPageInfo);
        Map m=new HashMap();
        m.put("total", monitorobjInfoMapper.queryMonitorobjCounts());
        m.put("rows", lists);
        return m;
    }

    @RequestMapping("/saveMonitorObj")
    @ResponseBody
    public void saveMonitorObj(MonitorobjInfo monitorobjInfo){
        String vaildResult=monitorobjInfo.vaildSelfRequriedAtrribute();
        if(!"success".equals(vaildResult)){
            throw new RuntimeException(vaildResult);
        }
        if(monitorobjInfo.getMonitorobjId()==null||"".equals(monitorobjInfo.getMonitorobjId())){
            //如果没有ID，则是新增
            monitorobjInfo.setMonitorobjId(BaseUtil.getUUID());
            monitorobjInfo.setCreatTime(new Date());
            monitorobjInfo.setRunStatus("stop");
            monitorobjInfoMapper.insert(monitorobjInfo);
        }else {
            //有ID则是，更新
            monitorobjInfoMapper.updateByPrimaryKeySelective(monitorobjInfo);
        }
    }

    @RequestMapping("/removeMonitorObj")
    @ResponseBody
    public void removeMonitorObj(String monitorobjId) {
        monitorobjInfoMapper.deleteByPrimaryKey(monitorobjId);
    }


    @RequestMapping("updateMonitorObj")
    @ResponseBody
    public void updateMonitorObj(MonitorobjInfo monitorobjInfo){
        monitorobjInfoMapper.updateByPrimaryKeySelective(monitorobjInfo);
    }

}
